package com.jiantong.web.open.app.analysis;

import com.google.common.collect.Maps;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.dto.analysis.ScaleBreedFactoryArchivesFormDto;
import com.jiantong.work.service.analysis.ScaleBreedFactoryArchivesFormReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 3/8/17.
 *
 */
@Slf4j
@OpenBean
public class ScaleBreedFactoryArchivesFormApis {

    @RpcConsumer
    private ScaleBreedFactoryArchivesFormReadService scaleBreedFactoryArchivesFormReadService;

    @OpenMethod(
            key = "get.analysis.archives.from",
            paramNames = {"breedFactoryId","date","type"},
            httpMethods = RequestMethod.GET
    )
    public ScaleBreedFactoryArchivesFormDto getAnalysisArchivesFrom(Long breedFactoryId,String date,Integer type){
        if(breedFactoryId == null || date == null || type == null){
            log.error("breedFactoryId、date and type should be not null");
            throw new OPClientException(ExceptionCode.PARAMETER_NOT_EXIST.value(),"breedFactoryId、date and type should be not null");
        }
        Map<String , Object> criteria = Maps.newHashMap();
        criteria.put("breedFactoryId",breedFactoryId);
        Date time = DateTime.parse(date).toDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;
        Integer day = calendar.get(Calendar.DAY_OF_MONTH);
        switch (type){
            case 1:
                criteria.put("year",year);
                criteria.put("month",month);
                criteria.put("day",day);
                break;
            case 2:
                criteria.put("date",time);
                break;
            case 3:
                criteria.put("year",year);
                criteria.put("month",month);
                break;
            case 4:
                criteria.put("year",year);
                criteria.put("month",month);
                break;
            default:
                break;
        }
        criteria.put("type",type);
        criteria.put("status",1);
        criteria.put("deleteFlag",1);
        Response<ScaleBreedFactoryArchivesFormDto> response =
                scaleBreedFactoryArchivesFormReadService.analysisArchives(criteria);

        if(!response.isSuccess()){
            log.error("failed to analysis archives form , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }
}
